package com.mosaic.girih;

import java.util.ArrayList;
import java.util.List;

import com.example.girih.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SettingsActivity extends MainActivity {
	private static EditText editTextOutFolder;
	private static EditText editTextOutFileName;
	private static CheckBox checkBoxFnAsIN;
	private static Spinner sFilters;
	private static Button bReset;
	private static Button bSave;
	private static Button bCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		refreshContent();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}

	// Refresh display
	public void refreshContent() {
		// Set default behavior option
		checkBoxFnAsIN = (CheckBox) findViewById(R.id.cb_default);
		checkBoxFnAsIN.setChecked(settings.getBoolean(enableDefaultKey, true));
		checkBoxFnAsIN
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							// Disable folder and file name fields
							editTextOutFolder.setEnabled(false);
							editTextOutFileName.setEnabled(false);
						} else {
							// Enable folder and file name fields
							editTextOutFolder.setEnabled(true);
							editTextOutFileName.setEnabled(true);
						}
					}
				});

		// Retrieve edit text objects
		editTextOutFolder = (EditText) findViewById(R.id.et_out_folder);
		editTextOutFileName = (EditText) findViewById(R.id.ev_out_name);

		if (checkBoxFnAsIN.isChecked()) {
			// Disable folder and file name fields
			editTextOutFolder.setEnabled(false);
			editTextOutFileName.setEnabled(false);
		} else {
			// Enable folder and file name fields
			editTextOutFolder.setEnabled(true);
			editTextOutFileName.setEnabled(true);
		}

		// Set text for output folder name
		editTextOutFolder.setText(settings.getString(outFolderKey, "output"));
		// Set text for output file name
		editTextOutFileName.setText(settings
				.getString(outNameKey, "default_output"));

		// Set list of available filters
		sFilters = (Spinner) findViewById(R.id.s_filter);
		List<String> listFilters = new ArrayList<String>();
		for(int i = 0; i < mFilters.length; i++)
			listFilters.add(mFilters[i].name);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listFilters);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sFilters.setAdapter(dataAdapter);
		sFilters.setSelection(settings.getInt(filterIdxKey, 0));

		// Set button to reset settings
		bReset = (Button) findViewById(R.id.b_reset);
		bReset.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// SharedPreferences.Editor editor = settings.edit();
				// Reset parameters
				checkBoxFnAsIN.setChecked(true);
				editTextOutFolder.setEnabled(false);
				editTextOutFileName.setEnabled(false);
				editTextOutFolder.setText("output");
				editTextOutFileName.setText("default_output");
				sFilters.setSelection(0);

				Toast.makeText(SettingsActivity.this,
						"Default parameters were restored", Toast.LENGTH_LONG)
						.show();
				// refreshContent();
			}
		});

		// Set button to save settings
		bSave = (Button) findViewById(R.id.b_save);
		bSave.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences.Editor editor = settings.edit();
				// Save parameters
				editor.putBoolean(enableDefaultKey, checkBoxFnAsIN.isChecked());
				editor.putString(outFolderKey, editTextOutFolder.getText()
						.toString());
				editor.putString(outNameKey, editTextOutFileName.getText()
						.toString());
				editor.putInt(filterIdxKey, sFilters.getSelectedItemPosition());
				editor.commit();
				finish();
			}
		});

		// Set button to cancel settings
		bCancel = (Button) findViewById(R.id.b_cancel);
		bCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Close activity
				finish();
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// When the user center presses back
			finish();
			return true;
		}
		return false;
	}

}
